package com.ef.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.EPayType;
import com.ef.constant.EContractState;
import com.ef.constant.StatusType;
import com.ef.domain.ClassUnit;
import com.ef.domain.Contract;
import com.ef.domain.Gift;
import com.ef.domain.Material;
import com.ef.domain.PayHistory;
import com.ef.service.ContractService;
import com.ef.service.GiftService;
import com.ef.service.MaterialService;
import com.ef.service.PayService;
import com.ef.util.CustomExample;

public class PersonalPayAction extends BaseAction<Contract> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(PersonalPayAction.class);

	private ContractService contractService;

	private PayService payService;

	private GiftService giftService;

	private MaterialService materialService;

	private List<ClassUnit> selectClassUnitList;

	private Map<String, String> allGifts;

	private Map<String, String> allMaterials;

	private Map<String, String> selectedGifts;

	private Map<String, String> selectedMaterials;

	private String selLeadsId;

	private String payMoney;

	private String payComment;
	
	private Date payTime;
	
	private String refMoney;
	
	private Date refTime;

	public Date getPayTime() {
		return payTime;
	}

	public void setPayTime(Date payTime) {
		this.payTime = payTime;
	}

	public Date getRefTime() {
		return refTime;
	}

	public void setRefTime(Date refTime) {
		this.refTime = refTime;
	}

	private String refComment;

	private List<PayHistory> payHistoryList;

	private String leadsName;

	private String leadsTel;
	
	@SuppressWarnings("serial")
	public String list() {
		this.listResult = contractService.findByExample(
				new CustomExample<Contract>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						if ((leadsName != null && leadsName.trim().length() > 0)
								|| ((leadsTel != null && leadsTel.trim()
										.length() > 0))) {
							criteria.createAlias("leads", "leads");
							if (leadsName != null
									&& leadsName.trim().length() > 0) {
								criteria.add(Restrictions.like(
										"leads.name", leadsName.trim(),
										MatchMode.ANYWHERE));
							}
							if (leadsTel != null
									&& leadsTel.trim().length() > 0) {
								Disjunction or = Restrictions.disjunction();
								Property myProperty = Property
										.forName("leads.telephone");
								or.add(myProperty.like(leadsTel.trim(),
										MatchMode.ANYWHERE));
								myProperty = Property
										.forName("leads.mobileTelephone");
								or.add(myProperty.like(leadsTel.trim(),
										MatchMode.ANYWHERE));
								myProperty = Property
										.forName("leads.companyTelephone");
								or.add(myProperty.like(leadsTel.trim(),
										MatchMode.ANYWHERE));
								myProperty = Property
										.forName("leads.otherTelephone");
								or.add(myProperty.like(leadsTel.trim(),
										MatchMode.ANYWHERE));
								criteria.add(or);
							}
						}
						criteria.add(Restrictions
								.eq("status", StatusType.VALID));
						criteria.addOrder(Order.desc("updateTime"));
						this.excludeProperty("isVip");
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		fetchListData();
		return EDIT;
	}
	
	public String view() {
		fetchListData();
		return VIEW;
	}

	private void fetchListData() {

		allGifts = new LinkedHashMap<String, String>();
		List<Gift> gfs = giftService.findALLVaild();
		for (Gift gf : gfs) {
			allGifts.put(gf.getId().toString(), gf.getName());
		}

		allMaterials = new LinkedHashMap<String, String>();
		List<Material> mats = materialService.findALLVaild();
		for (Material mat : mats) {
			allMaterials.put(mat.getId().toString(), mat.getName());
		}

		selectedGifts = new LinkedHashMap<String, String>();
		if (this.getModel() != null && this.getModel().getGifts() != null) {
			List<Gift> tgifts = this.getModel().getGifts();
			for (Gift gift : tgifts) {
				selectedGifts.put(gift.getId().toString(), gift.getName());
			}
		}
		selectedMaterials = new LinkedHashMap<String, String>();
		if (this.getModel() != null && this.getModel().getMaterials() != null) {
			List<Material> tMaterials = this.getModel().getMaterials();
			for (Material material : tMaterials) {
				selectedMaterials.put(material.getId().toString(), material
						.getName());
			}
		}

		if (this.getModel().getId() != null
				&& this.getModel().getClassUnits() != null) {
			selectClassUnitList = this.getModel().getClassUnits();

		} else {

			selectClassUnitList = new ArrayList<ClassUnit>();
		}

		if (this.getModel().getId() != null
				&& this.getModel().getLeads() != null) {
			this.selLeadsId = this.getModel().getLeads().getId().toString();
		}

		if (this.getModel().getId() != null) {
			payHistoryList = payService.findByContract(this.getModel());
		}

	}

	public String pay() {
		Contract cont = this.getModel();
		PayHistory pay = new PayHistory();
		pay.setComments(this.payComment);
		pay.setPay(Double.valueOf(payMoney.trim()));
		pay.setContract(cont);
		pay.setPayTime(this.payTime);
		pay.setPayType(EPayType.Pay);
		pay.setPayUser(getSessionUser());
		payService.saveOrUpdate(pay);
		if (cont.getPaidMoney() == null) {
			cont.setPaidMoney(Double.valueOf(payMoney.trim()));
		} else {
			cont.setPaidMoney(Double.valueOf(payMoney.trim())
					+ cont.getPaidMoney());
		}
		contractService.saveOrUpdate(cont);
		fetchListData();
		return VIEW;
	}

	public String refund() {
		Contract cont = this.getModel();
		
		PayHistory pay = new PayHistory();
		pay.setComments(this.refComment);
		pay.setPay(Double.valueOf(refMoney.trim()));
		pay.setContract(cont);
		pay.setPayTime(this.refTime);
		pay.setPayType(EPayType.Refund);
		pay.setPayUser(getSessionUser());
		payService.saveOrUpdate(pay);
		if (cont.getPaidMoney() == null) {
			cont.setPaidMoney(0d);
		} else {
			cont.setPaidMoney(cont.getPaidMoney()
					- Double.valueOf(refMoney.trim()));
		}

		cont.setContractState(EContractState.REFUND);
		contractService.saveOrUpdate(cont);
		fetchListData();
		return VIEW;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public PayService getPayService() {
		return payService;
	}

	public void setPayService(PayService payService) {
		this.payService = payService;
	}

	public GiftService getGiftService() {
		return giftService;
	}

	public void setGiftService(GiftService giftService) {
		this.giftService = giftService;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	public List<ClassUnit> getSelectClassUnitList() {
		return selectClassUnitList;
	}

	public void setSelectClassUnitList(List<ClassUnit> selectClassUnitList) {
		this.selectClassUnitList = selectClassUnitList;
	}

	public Map<String, String> getAllGifts() {
		return allGifts;
	}

	public void setAllGifts(Map<String, String> allGifts) {
		this.allGifts = allGifts;
	}

	public Map<String, String> getAllMaterials() {
		return allMaterials;
	}

	public void setAllMaterials(Map<String, String> allMaterials) {
		this.allMaterials = allMaterials;
	}

	public Map<String, String> getSelectedGifts() {
		return selectedGifts;
	}

	public void setSelectedGifts(Map<String, String> selectedGifts) {
		this.selectedGifts = selectedGifts;
	}

	public Map<String, String> getSelectedMaterials() {
		return selectedMaterials;
	}

	public void setSelectedMaterials(Map<String, String> selectedMaterials) {
		this.selectedMaterials = selectedMaterials;
	}

	public String getSelLeadsId() {
		return selLeadsId;
	}

	public void setSelLeadsId(String selLeadsId) {
		this.selLeadsId = selLeadsId;
	}

	public String getPayMoney() {
		return payMoney;
	}

	public void setPayMoney(String payMoney) {
		this.payMoney = payMoney;
	}

	public List<PayHistory> getPayHistoryList() {
		return payHistoryList;
	}

	public void setPayHistoryList(List<PayHistory> payHistoryList) {
		this.payHistoryList = payHistoryList;
	}

	public String getRefMoney() {
		return refMoney;
	}

	public void setRefMoney(String refMoney) {
		this.refMoney = refMoney;
	}

	public String getRefComment() {
		return refComment;
	}

	public void setRefComment(String refComment) {
		this.refComment = refComment;
	}

	public String getPayComment() {
		return payComment;
	}

	public void setPayComment(String payComment) {
		this.payComment = payComment;
	}

	public String getLeadsName() {
		return leadsName;
	}

	public void setLeadsName(String leadsName) {
		this.leadsName = leadsName;
	}

	public String getLeadsTel() {
		return leadsTel;
	}

	public void setLeadsTel(String leadsTel) {
		this.leadsTel = leadsTel;
	}

}
